import axios from "axios"
import {getToken,removeToken} from '@/utils/auth.js'

// axios.defaults.headers.post['Content-type'] = 'application'

export function request(config) {
    const instance = axios.create({
        
        baseURL: 'http://127.0.0.1:3000',
        timeout:5000,


    });

    // 有token携带token
    instance.interceptors.request.use(config => {
        // do something before request is sent
    
        if (getToken()) {
          config.headers['Authorization'] = 'Bearer ' + getToken();
        }
        return config
      },
      error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
      })
    instance.interceptors.request.use(config => {
        return config
    }, err => {

    })

    instance.interceptors.response.use(res => {
        // code401 token错误或失效
        if (res.data.errno === 401){
            // 删除当前token
            removeToken();
        }
        return res.data
    }, err =>{
        console.log(err)
    })

    return instance(config)
}